<?php
namespace Home\Controller;
use Think\Controller;

Class TjbiaoController extends CommonController{

    public function _initialize() {
        parent::_initialize();
        $this->dbname = CONTROLLER_NAME;
    }

   public function index(){
    //获取当前访问的月份
        if(IS_POST){
          $time = strtotime($_REQUEST['time1']);
        }else{
          $time = time();
        }
        $ym = date('Y-m',$time);
        $where['year_month'] = $ym;
        $val = M('cash_order_monthly')->where($where)->select();
        if(count($val) == 3){
          $status = 1;
        }else{
          $status = 2;
        }
        $this->details($time,$status);
        $this->assign('time',$time);
        $this->display();
   }

  //已10月份为例  统计上旬 中旬  下旬个阶段的订单详情
  //上旬:1号0时————11号0时  11号0时--21号0时  21号0时————下月1号的0时
   public function details($time,$status){
    //获取当前的年和月
    $ym = date('Y-m',$time);
    //获取要查询的月份的第一天的时间戳
    $startday = date('Y-m-01',$time);

    //获取0点的时间戳                          
    $str = strtotime($startday);

    //获取上旬结束的时间戳   11：00
    $str2 = $str + 86400*10;

    //获取中旬结束的时间戳
    $str3 = $str2 + 86400*10;
    //var_dump(date('Y-m-d H:i:s',$str3));exit;
    
    //获取本月最后一天的时间戳   date('Y-m-d', strtotime("$BeginDate +1 month -1 day"));
    $ymd = date('Y-m-d',strtotime("$startday +1 month"));
    $str4 = strtotime($ymd) + 86400;
    //每月上旬放款订单的回款情况
    $shangxun = $this->shangxun($str,$str2);

    //获取每月中旬的数据
    $zhongxun = $this->shangxun($str2,$str3);

    //获取每个月的下旬的数据
    $xiaxun = $this->shangxun($str3,$str4);
    if($status == 1){
      //修改操作
      $this->UP($ym,$shangxun,$zhongxun,$xiaxun);
    }else{
      //添加操作
      $this->tianjia($ym,$shangxun,$zhongxun,$xiaxun);
    }
   
   }

   //添加操作
   public function tianjia($ym,$shangxun,$zhongxun,$xiaxun){
    $add['creat_time'] = time();
    $add['year_month'] = $ym;
    //循环操作将获的到的数据添加道数据库中
    for($i=1;$i<4;$i++){
      if($i == 1){
        $add['huikan'] = $shangxun['Hzong'];
        $add['xuqi'] = $shangxun['Hxuqi'];
        $add['yuqi'] = $shangxun['Hyuqi'];
        $add['bad_debt'] = $xiaxun['Hbad'];
        $add['data_time'] = 1;
        $add['yuqi_never'] = $shangxun['yuqi_never'];
        $add['yuqi_hrecord'] = $shangxun['yuqi_hrecord'];
        $add['bad_never'] = $shangxun['bad_never'];
        $add['bad_hrecord'] = $shangxun['bad_hrecord'];
      }
      if($i == 2){
        $add['huikuan'] = $zhongxun['Hzong'];
        $add['xuqi'] = $zhongxun['Hxuqi'];
        $add['yuqi'] = $zhongxun['Hyuqi'];
        $add['bad_debt'] = $xiaxun['Hbad'];
        $add['data_time'] = 2;
        $add['yuqi_never'] = $zhongxun['yuqi_never'];
        $add['yuqi_hrecord'] = $zhongxun['yuqi_hrecord'];
        $add['bad_never'] = $zhongxun['bad_never'];
        $add['bad_hrecord'] = $zhongxun['bad_hrecord'];
      }
      if($i == 3){
        $add['huikuan'] = $xiaxun['Hzong'];
        $add['xuqi'] = $xiaxun['Hxuqi'];
        $add['yuqi'] = $xiaxun['Hyuqi'];
        $add['bad_debt'] = $xiaxun['Hbad'];
        $add['data_time'] = 3;
        $add['yuqi_never'] = $xiaxun['yuqi_never'];
        $add['yuqi_hrecord'] = $xiaxun['yuqi_hrecord'];
        $add['bad_never'] = $xiaxun['bad_never'];
        $add['bad_hrecord'] = $xiaxun['bad_hrecord'];
      }
         $result = M('cash_order_monthly')->add($add);
    }
   }

   //修改操作
   public function UP($ym,$shangxun,$zhongxun,$xiaxun){
        $where['year_month'] = $ym;
        for($i=1;$i<4;$i++){
          if($i == 1){
            $add['huikuan'] = $shangxun['Hzong'];
            $add['xuqi'] = $shangxun['Hxuqi'];
            $add['yuqi'] = $shangxun['Hyuqi'];
            $add['bad_debt'] = $shangxun['Hbad'];
            $add['creat_time'] = time();
            $where['data_time'] = 1;
            $add['yuqi_never'] = $shangxun['yuqi_never'];
            $add['yuqi_hrecord'] = $shangxun['yuqi_hrecord'];
            $add['bad_never'] = $shangxun['bad_never'];
            $add['bad_hrecord'] = $shangxun['bad_hrecord'];
          }
          if($i == 2){
            $add['huikuan'] = $zhongxun['Hzong'];
            $add['xuqi'] = $zhongxun['Hxuqi'];
            $add['yuqi'] = $zhongxun['Hyuqi'];
            $add['bad_debt'] = $zhongxun['Hbad'];
            $add['creat_time'] = time();
            $where['data_time'] = 2;
            $add['yuqi_never'] = $zhongxun['yuqi_never'];
            $add['yuqi_hrecord'] = $zhongxun['yuqi_hrecord'];
            $add['bad_never'] = $zhongxun['bad_never'];
            $add['bad_hrecord'] = $zhongxun['bad_hrecord'];
          }
          if($i == 3){
            $add['huikuan'] = $xiaxun['Hzong'];
            $add['xuqi'] = $xiaxun['Hxuqi'];
            $add['yuqi'] = $xiaxun['Hyuqi'];
            $add['bad_debt'] = $xiaxun['Hbad'];
            $add['creat_time'] = time();
            $where['data_time'] = 3;
            $add['yuqi_never'] = $xiaxun['yuqi_never'];
            $add['yuqi_hrecord'] = $xiaxun['yuqi_hrecord'];
            $add['bad_never'] = $xiaxun['bad_never'];
            $add['bad_hrecord'] = $xiaxun['bad_hrecord'];
          }
          $result = M('cash_order_monthly')->where($where)->save($add);
        }
   }

   //获取指定月份的上旬时间端内的回款订单数
   public function shangxun($str,$str2){

        $result = M('cash_order')->where("status = 5 AND loan_time >= {$str} AND loan_time < {$str2}")->count();

        //统计出当前时间段内status=4的订单
        $zong = M('cash_order')->where("status = 4 AND loan_time>={$str} AND loan_time<{$str2}")->select();

        //划分出有记录  和  无记录的
        foreach($zong as $k=>$v){

          $val = M('cash_bf_dk')->where("order_id = {$v['order_id']} AND status = 'S'")->order('k_time desc')->select();

          if(is_array($val)){ $arr['xuqi'][] = $v;$arr['bf_dk'][] = $val[0];}else{ $arr['no'][] = $v;}
        }

        $neverR = $this->neverR($arr['no']);
        //$Hrecord = $this->Hrecord($arr['bf_dk']);
        $Hrecord = $this->Hrecord($arr['xuqi']);
        //该时间段内回款量
        $array['Hzong'] = $result;
        $array['Hxuqi'] = count($neverR['zc']) + count($Hrecord['zc']);
        $array['Hyuqi'] = count($neverR['yu']) + count($Hrecord['yu']);
        $array['Hbad'] = count($neverR['bad']) + count($Hrecord['bad']);

        $array['yuqi_never'] = count($neverR['yu']);
        $array['yuqi_hrecord'] = count($Hrecord['yu']);
        $array['bad_never'] = count($neverR['bad']);
        $array['bad_hrecord'] = count($Hrecord['bad']);
//echo $array['yuqi_never'].'++++'.$array['yuqi_hrecord'];
        return $array;
   }

   //查看没有订单记录的单子    逾期或未逾期
   public function neverR($val){
       //当前时间 - 放款时间   和天数做比较    >逾期   <正常
       $time = time();
       foreach($val as $k=>$v){
        $dayC = (($time - $v['loan_time']) / 86400);
        $day = $dayC - $v['days'];
        if($day<=0){
          $arr['zc'][] = $v;
        }elseif($day>0 && $day<45){
          $arr['yu'][] = $v;
        }elseif($day>=45){
          $arr['bad'][] = $v;
        }
      } 
      return $arr;
   }

   //查看有交易记录的订单  获取最后交易的订单  k_time 和当前时间做比较   over_day

    public function Hrecord($val){
        $time = time();   
        foreach($val as $k=>$v){
          //计算当前时间和订单预估到期时间的间隔天数
          $dayC = (($time - $v['expire_time']) / 86400);
          if($dayC<=0){
            //订单正常
            $arr['zc'] = $v;
          }elseif($dayC>0 && $dayC<45){
            //订单逾期
            $arr['yu'] = $v;
          }else{
            //订单超出45天
            $arr['bad'] = $v;
          }
        }
        //echo count($arr['zc']).'++++'.count($arr['yu']).'++++'.count($arr['bad']);
        return $arr;
    }

   // public function Hrecord($val){

   //  $time = time();
   //  foreach($val as $k=>$v){
   //      //计算当前时间和订单处理时间之间的天数
   //       $dayC = (($time - $v['k_time']) /86400 );
   //       $day = $dayC - $v['over_day'];
   //       if($day<=0){
   //        $arr['zc'][] = $v;
   //      }elseif($day>0 && $day<45){
   //        $arr['yu'][] = $v;
   //      }elseif($day>=45){
   //        $arr['bad'][] = $v;
   //      }
   //  }
   //  return $arr;
   // }

   //图形
   public function type(){
    $ym = date('Y-m',$_REQUEST['time1']);
    if($_REQUEST['type'] == 1){
      $where['data_time'] = 1;
      $title = '上旬统计';
    }
    if($_REQUEST['type'] == 2){
      $where['data_time'] = 2;
      $title = '中旬统计';
    }
    if($_REQUEST['type'] == 3){
      $where['data_time'] = 3;
      $title = '下旬统计';
    }
    $where['year_month'] = $ym;
    $result = M('cash_order_monthly')->where($where)->field('huikuan,xuqi,yuqi,bad_debt')->find();
    foreach($result as $k=>$v){
      $legend[] = $v;
    }
    $data = array('回款','续期','逾期','死掉');
    $this->yqfenxi('type',$title,2,$legend,$data);
   }

   //折线统计图
   public function type2(){
       $date = date('Y-m',$_REQUEST['time1']);
       $where['year_month'] = $date;
       $result = M('cash_order_monthly')->where($where)->select();
       $data = array('回款','续期','逾期','死掉');
       $huikuan = $result[0]['huikuan'] + $result[1]['huikuan'] + $result[2]['huikuan'];
       $xuqi = $result[0]['xuqi'] + $result[1]['xuqi'] + $result[2]['xuqi'];
       $yuqi = $result[0]['yuqi'] + $result[1]['yuqi'] + $result[2]['yuqi'];
       $bad = $result[0]['bad_debt'] + $result[1]['bad_debt'] + $result[2]['bad_debt'];
       $legend = array($huikuan,$xuqi,$yuqi,$bad);
       $this->yqfenxi('type','整月统计',3,$legend,$data);
   }

   //上旬
   public function type_yuqi(){
      //获取当前的年月
      $ym = date('Y-m',$_REQUEST['time1']);
      $where['year_month'] = $ym;
      $where['data_time'] = $_REQUEST['types'];
      $result = M('cash_order_monthly')->where($where)->field('yuqi_never,yuqi_hrecord,bad_never,bad_hrecord')->find();
      if($_REQUEST['type'] == 3){
        //逾期操作
        $title = '逾期详情';
        $val = array($result['yuqi_never'],$result['yuqi_hrecord']);
      }else{
        //死掉操作
        $title = '死掉详情';
        $val = array($result['bad_never'],$result['bad_hrecord']);
      }
      $data = array('没有记录','有记录');
      $this->yqfenxi('type',$title,2,$val,$data);

   }

  public function yqfenxi($fd,$ft,$type,$data,$legend) {
    //var_dump($data);exit;
      import("Org.Util.Chart");
      $chart = new \Chart;
      $title = $ft; 
      $size = 140; 
      $width = 550; 
      $height = 300;    
      ob_end_clean();
      if ($type == 1) {
        $chart->create3dpie($title,$data,$size,$height,$width,$legend);
      }
      if ($type == 2) {
        $chart->createcolumnar($title,$data,$size,$height,$width,$legend);
      }
      if ($type == 3) {
        $chart->createmonthline($title,$data,$size,$height,$width,$legend);
      }
      if ($type == 4) {
        $chart->createring($title,$data,$size,$height,$width,$legend);
      }
      if ($type == 5) {
        $chart->createhorizoncolumnar($title,$subtitle,$data,$size,$height,$width,$legend);
  }
   
  }

}